package com.yytek.common.core.utils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CronUtil {
    /**
     * 是否匹配仅一次时间执行cron
     * @param str
     * @return
     */
    public static boolean isTimeCron(String str) {
        String pattern = "[0-9]{2}\\s[0-9]{2}\\s[0-9]{2}\\s[0-9]{2}\\s[0-9]{2}\\s[?]\\s[0-9]{4}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        return m.matches();
    }

    /**
     * 比较是否在当前时间之前,是否过期时间
     * @param cornStr
     * @return
     */
    public static boolean isBeforeNowTime(String cornStr) {
        LocalDateTime dateTime = onlyOnceOverTime(cornStr);
        LocalDateTime now = LocalDateTime.now();
        if(dateTime.isAfter(now)){
            return false;
        }else if(dateTime.isBefore(now)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 仅一次反格式化时间
     * @return
     */
    public static LocalDateTime onlyOnceOverTime(String cronStr) {
        LocalDateTime time = LocalDateTime.parse(cronStr, DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy"));
        return time;
    }

    /**
     * 仅一次
     *
     * @param dateStr   2022-03-31 09:51:05
     * @return
     */
    public static String onlyOnce(String dateStr) {
        LocalDateTime time = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy"));
        return cronStr;
    }

    /**
     * 每天
     * @param timeStr   09:51:05
     * @return
     */
    public static String everyDay(String timeStr) {
        LocalTime time = LocalTime.parse(timeStr, DateTimeFormatter.ofPattern("HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH * * ?"));
        return cronStr;
    }

    /**
     * 每周
     * @param timeStr   09:51:05
     * @param week      1代表周日 7代表周六 可选值：1、2、3、4、5、6、7、1-5、1-7
     * @return
     */
    public static String everyWeek(String timeStr, String week) {
        LocalTime time = LocalTime.parse(timeStr, DateTimeFormatter.ofPattern("HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH ? * " + week));
        return cronStr;
    }

    /**
     * 每月
     * @param dateStr   2022-03-31 09:51:05
     * @return
     */
    public static String everyMonth(String dateStr) {
        LocalDateTime time = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH dd * ?"));
        return cronStr;
    }

    /**
     * 每年
     * @param dateStr   2022-03-31 09:51:05
     * @return
     */
    public static String everyYear(String dateStr) {
        LocalDateTime time = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String cronStr = time.format(DateTimeFormatter.ofPattern("ss mm HH dd MM *"));
        return cronStr;
    }

}
